/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.fraction;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.sample.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * RepeatLoadOrDownloadTestFraction
 */
public class RepeatLoadOrDownloadTestFraction extends Fraction {

    private List<SketchImageView> mSketchImageViews;
    private DisplayOptions mDisplayOptions;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_repeat_load_or_download_test_fraction, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initComponent();
        initData();
    }

    private void initComponent() {
        mSketchImageViews = new ArrayList<>();

        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_1));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_2));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_3));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_4));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_5));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_6));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_7));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_8));

        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_9));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_10));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_11));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_12));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_13));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_14));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_15));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_16));

        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_31));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_32));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_33));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_34));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_35));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_36));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_37));
        mSketchImageViews.add((SketchImageView) getComponent().findComponentById(ResourceTable.Id_image_repeatLoadOrDownloadTest_38));

    }

    private void initData() {
        String appIconPath = getFractionAbility().getApplicationInfo().getIcon();
        int localImageSize = 8;
        int allImageSize = mSketchImageViews.size();
        if ("$media:ic_launcher".equals(appIconPath)) {
            for (int i = 0; i < localImageSize; i++) {
                mSketchImageViews.get(i).setOptions(mDisplayOptions);
                Sketch.with(getFractionAbility()).displayFromResource(ResourceTable.Media_ic_launcher, mSketchImageViews.get(i))
                        .commit();
            }
        }
        String url = null;
        try {
            url = getFractionAbility().getResourceManager().getElement(ResourceTable.String_repeateUrl).getString();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        for (int i = localImageSize; i < allImageSize; i++) {
            Sketch.with(getFractionAbility()).display(url, mSketchImageViews.get(i))
                    .commit();
        }
    }

}
